using System;
using System.Collections.Generic;
using System.Linq;
using SkypeStudio.Framework;
using SkypeStudio.Controls.Views;
using SkypeStudio.Controls.Services;
using System.Collections.ObjectModel;
using SkypeStudio.Controls.Model;
using System.Windows.Input;

namespace SkypeStudio.Controls.ViewModels
{
    public class SendMessageViewModel : ViewModelBase<ISendMessageView>, IDisposable
    {

        private ISkypeApplicationService _SkypeService;
        private  void OnRequestBackToChatList(Chat chat)
        {
            Action<Chat> handler = RequestBackToChatList;
            if (handler != null)
                handler(chat);
        }
        public event Action<Chat> RequestBackToChatList;


        public SendMessageViewModel(ISendMessageView view, ISkypeApplicationService skypeService)
            : base(view)
        {
            if (skypeService == null)
                throw new ArgumentNullException("skypeService");

            _SkypeService = skypeService;
            //_SkypeService.MessageReceived += SkypeServiceMessageReceived;
            //_SkypeService.MessageSending += SkypeServiceMessageSending;


            // _ChatMessages = new ObservableCollection<ChatMessage>();
        }

        void SkypeServiceMessageReceived(ChatMessage msg)
        {
            // TODO: Validate the chat message should be add to the this chat window or not
            //ChatMessages.Add(msg);
        }
        void SkypeServiceMessageSending(ChatMessage msg)
        {
            // TODO: Validate the chat message should be add to the this chat window or not
            //ChatMessages.Add(msg);
        }

        public void Dispose()
        {
            if (_SkypeService != null)
            {
                _SkypeService.MessageReceived -= SkypeServiceMessageReceived;
                this._SkypeService = null;
            }
        }

        private Chat _ChatSource;
        public Chat ChatSource
        {
            get
            {
                return _ChatSource;
            }
            set
            {
                if (_ChatSource == value)
                    return;
                _ChatSource = value;


                NotifyOfPropertyChange(() => ChatSource);
                NotifyOfPropertyChange(() => ChatName);
                NotifyOfPropertyChange(() => ChatMessages);
                NotifyOfPropertyChange(() => Friends);

            }
        }

        public string ChatName
        {
            get
            {
                if (ChatSource == null)
                    return string.Empty;

                return ChatSource.ChatName;
            }
        }

        public ObservableCollection<ChatMessage> ChatMessages
        {
            get
            {
                if (ChatSource == null)
                    return new ObservableCollection<ChatMessage>();

                return ChatSource.ChatMessages;
            }
            //set
            //{
            //    if (_ChatMessages == value)
            //        return;
            //    _ChatMessages = value;
            //    NotifyOfPropertyChange(() => ChatMessages);
            //}
        }

        private string _SendText;
        public string SendText
        {
            get
            {
                return _SendText;
            }
            set
            {
                if (_SendText == value)
                    return;
                _SendText = value;

                NotifyOfPropertyChange(() => SendText);
            }
        }

        private ICommand _SendMessageCommand;
        public ICommand SendMessageCommand
        {
            get
            {
                if (_SendMessageCommand == null)
                {
                    _SendMessageCommand = new RelayCommand(SendMessageToFriends, (o) => { return !string.IsNullOrEmpty(this.SendText); });
                }
                return _SendMessageCommand;
            }
        }

        public ObservableCollection<Contact> Friends
        {
            get
            {
                if (ChatSource == null)
                    return new ObservableCollection<Contact>();
                return ChatSource.Members;
            }
        }

        public void SendMessageToFriends(object param)
        {
            if (this.Friends == null)
                return;

            if (string.IsNullOrEmpty(this.SendText))
                return;

            if (this.ChatSource == null)
                return;

            this._SkypeService.SendMessage(this.ChatSource, this.SendText);
            

            this.SendText = string.Empty;
        }


        private ICommand _BackToChatListCommand;
        public ICommand BackToChatListCommand
        {
            get
            {
                if (this._BackToChatListCommand == null)
                {
                    _BackToChatListCommand = new RelayCommand(BackToChatList);
                }
                return _BackToChatListCommand;
            }

        }

        private void BackToChatList(object param)
        {
            OnRequestBackToChatList(this.ChatSource);
        }


    }
}
